<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>">
    <style type="text/css">

    </style>

</head>
<body>
<div class="page-return">
    <div class="ops-form">
        <div class="page-head">
            <form class="form-horizontal tasi-form" method="post" id="params" action="">
                <div class="form-group">
                    <label class="col-sm-1 control-label">业务字段</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" name="businessKey" id="businessKey" value="">
                    </div>
                    <label class="col-sm-1 control-label">流程名称</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" name="name" id="name" value="">
                    </div>
                    <div class="col-sm-3">
                        <button type="button" class="btn btn-info" onclick="" id="runtimeProcessSearch">搜索</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="ops-table" >
        <caption>
            <div class="ops-edit">
            </div>
        </caption>
        <div class="ops-table-content">
            <table id="runtimeProcessTable"></table>
        </div>
    </div>
</div>
</body>
<script>
    $(function() {
        var runtimeProcessTable = $("#runtimeProcessTable").bootstrapTable({ // 对应table标签的id
            url: "/workflow/runtimeprocess/list", // 获取表格数据的url
            queryParams: function (params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
                var paramOption = {
                    pageSize: params.limit, // 每页要显示的数据条数
                    pageNumber: (params.offset / params.limit) + 1, // 每页显示数据的开始行号
                    sortName: params.sort, // 要排序的字段
                    searchText: '',//搜索内容
                    sortOrder: params.order, // 排序规则
                    dataId: $("#dataId").val() // 额外添加的参数
                }
                return paramOption;
            },
            columns: [
                {
                    field: 'name',
                    title: '流程名称',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center'
                },
                {
                    field: 'businessKey',
                    title: '业务字段',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center'
                },
                {
                    field: 'startUser',
                    title: '发起人',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center'
                },
                {
                    field: 'taskName',
                    title: '当前任务',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center',
                },
                {
                    field: 'assignee',
                    title: '当前任务办理者',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center',
                    formatter: function (value, row, index) {
                        if(value.length<=8){
                            return value;
                        }else{
                            return  value.substring(0,8)+'...'+'<a onclick="details(\''+value+'\')" style="cursor:pointer;" title="详情"><i id="nav-search-icon" class="ace-icon fa fa-search bigger-110 nav-search-icon blue"></i>';
                        }
                    }
                },
                {
                    field: 'startTime',
                    title: '发起时间',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center',
                    formatter: function (value, row, index) {
                        return new Date(value).format("yyyy-MM-dd hh:mm:ss");
                    }
                },
                {
                    field: 'isSuspended',
                    title: '状态',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center',
                    formatter: function (value, row, index) { // 单元格格式化函数
                        var text = '';
                        if (row.isSuspended) {
                            text = '<h7 style="color:red">已挂起</h7>';
                        } else {
                            text = '<h7 style="color:green">正在运行</h7><img src="public/images/canvas/runing.gif" width="12px;" />';
                        }
                        return text;
                    }
                },
                {
                    title: "操作",
                    width: 240, // 定义列的宽度，单位为像素px
                    formatter: function (value, row, index) {
                        if (row.isSuspended) {
                            return '<button class="btn btn-xs btn-info" onclick="viewProcess(\'' + row.excuseId + '\')">流程信息</button> &nbsp;'
                                + '<button class="btn btn-xs btn-warning" onclick="onoffprocess(\'' + row.processInstanceId + '\',\'1\')"><i class="ace-icon glyphicon glyphicon-play" title="激活"></i></button> &nbsp;'
                                + '<button class="btn btn-xs btn-info" onclick="reCandidateUser(\'' + row.excuseId + '\',\'1\')">办理人</i></button> &nbsp;'
                                + '<button class="btn btn-xs btn-danger" onclick="delprocess(\'' + row.processInstanceId + '\')">作废</button>   ';
                        } else {
                            return '<button class="btn btn-xs btn-info" onclick="viewProcess(\'' + row.excuseId + '\')">流程信息</button> &nbsp;'
                                + '<button class="btn btn-xs btn-warning" onclick="onoffprocess(\'' + row.processInstanceId + '\',\'2\')"><i class="ace-icon glyphicon glyphicon-off" title="挂起"></i></button> &nbsp;'
                                + '<button class="btn btn-xs btn-info" onclick="reCandidateUser(\'' + row.excuseId + '\',\'1\')">办理人</button> &nbsp;'
                                + '<button class="btn btn-xs btn-danger" onclick="delprocess(\'' + row.processInstanceId + '\')">作废</button>';
                        }
                    }
                }
            ]
        });
    });

    /**
     * 流程实例的挂起和激活，流程挂起后无法继续任务，流程激活后可以执行任务
     * @param definedId 流程定义id
     * @param status 状态 1代表激活，2，代表挂起
     */
    function onoffprocess(processInstanceId,status) {
        var str=(status==1?"激活":"挂起");
        layer.confirm('流程挂起后,任务无法继续，直到流程再次被激活！<br> 确认'+str+'?', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type:"POST",
                url:'/workflow/runtimeprocess/onoff',
                data: {processInstanceId:processInstanceId,isSuspended:status},
                dataType:"json",
                success:function(data){
                    if(data.resCode == '200'){
                        parent.$("#runtimeProcessTable").bootstrapTable('refresh',{silent: true});
                        parent.layer.msg(data.resMessage,{time:1200});
                        return true;
                    }else {
                        parent.layer.msg(data.resMessage);
                        return false;
                    }
                },
                error:function(data){
                    parent.layer.msg("错误："+data.resMessage);
                }
            })
        }, function(){
            layer.msg('取消操作', {
                time: 1500 //1.5s后自动关闭
            });
        });
    }

    /**
     * 作废流程
     * @param processInstanceId 流程实例id
     */
    function delprocess(processInstanceId) {
        layer.confirm('作废的流程无法恢复，谨慎操作，是否确认作废流程', {
            btn: ['确定','取消'] //按钮
        }, function(){
            var index = layer.prompt({
                title : '请输入作废缘由！'
            },function(val, index){
                $.ajax({
                    type:"POST",
                    url:'/workflow/runtimeprocess/delete',
                    data: {processInstanceId:processInstanceId,reason:val},
                    dataType:"json",
                    success:function(data){
                        if(data.resCode == '200'){
                            parent.$("#runtimeProcessTable").bootstrapTable('refresh',{silent: true});
                            parent.layer.msg(data.resMessage,{time:1200});
                            layer.close(index);
                            return true;
                        }else {
                            parent.layer.msg(data.resMessage);
                            return false;
                        }
                    },
                    error:function(data){
                        parent.layer.msg("错误："+data.resMessage);
                    }
                })
            });

        }, function(){
            layer.msg('取消操作', {
                time: 1500 //1.5s后自动关闭
            });
        });
    }

    /**
     * 根据任务的执行Id重新分类任务的候选人
     * @param excuseId 任务的执行id
     */
    function reCandidateUser(excuseId){
        layer.confirm('此操作会重新分配流程当前运行的任务节点的候选办理人。<br/>是否继续？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            var index = layer.prompt({
                title : '请输入候选人名单，逗号间隔！'
            },function(val, index){
                $.ajax({
                    type:"POST",
                    url:'/workflow/runtimeprocess/candidateUser',
                    data: {excusedId:excuseId,users:val},
                    dataType:"json",
                    success:function(data){
                        if(data.resCode == '200'){
                            parent.$("#runtimeProcessTable").bootstrapTable('refresh',{silent: true});
                            parent.layer.msg(data.resMessage,{time:1200});
                            layer.close(index);
                            return true;
                        }else {
                            parent.layer.msg(data.resMessage);
                            return false;
                        }
                    },
                    error:function(data){
                        parent.layer.msg("错误："+data.resMessage);
                    }
                })
            });

        }, function(){
            layer.msg('取消操作', {
                time: 1500 //1.5s后自动关闭
            });
        });
    }

    /**
     * 预览流程
     * @param excuseId 流程的执行id
     */
    function viewProcess(excuseId){
        layer.open({
            type: 2,
            title: '流程信息',
            shadeClose: true,
            shade: false,
            maxmin: true,
            area: ['1100px', '599px'],
            content: '/workflow/runtimeprocess/processinfo/?excuseId='+excuseId
        });
    }

    /**
     * 查看内容详情
     * @param content 内容
     */
    function details(content) {
        layer.open({
            type: 2,
            title: '详细信息',
            shadeClose: true,
            shade: false,
            maxmin: true,
            area: ['760px', '500px'],
            content: '/global/details.jsp?details='+content
        });
    }
    /**
     *  搜索按钮
     */
    $("#runtimeProcessSearch").click(function () {
        $("#runtimeProcessTable").bootstrapTable('refresh',{
            silent: true,
            query: {
                name:$("#name").val(),
                businessKey:$("#businessKey").val()
            }
        });
    });
</script>
</html>